package com.vcredit.creditcat.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vcredit.bird.api.IBankCardService;
import com.vcredit.bird.enumtype.BankEnum;
import com.vcredit.bird.enumtype.CreditCardBankEnum;
import com.vcredit.bird.vo.custcard.BankCardBin;
import com.vcredit.creditcat.dto.CustCardDto;
import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.enumtype.BankCardTypeEnum;
import com.vcredit.creditcat.enumtype.CardStatusEnum;
import com.vcredit.creditcat.exception.CustCardExcetpion;
import com.vcredit.creditcat.manager.CreditCatProcessManager;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.AuthLog;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustCard;
import com.vcredit.creditcat.persistence.base.CustCardMapper;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.EnumUtil;

/**
 * 
 **/
@Service
public class CustCardService {
	@Autowired
	private IBankCardService bankCardService;
	@Autowired
	private CreditCatProcessManager creditCatProcessManager;
	@Autowired
	private AuthLogService authLogService;
	
	@Value("${card.four.guid}")
    private String source;
	
	@Autowired
	private CustCardMapper mapper;

	public void insert(CustCard obj) {
		mapper.insert(obj);
	}

	public void update(CustCard obj) {
		mapper.update(obj);
	}

	public List<CustCard> findAll() {
		return mapper.findAll();
	}

	public List<CustCard> find(CustCard obj) {
		return mapper.find(obj);
	}

	public List<CustCard> findByCustId(Long custId) {
		CustCard card = new CustCard();
		card.setCustId(custId);
		card.setCardStatus(CardStatusEnum.Valid.getCode());
		return this.find(card);
	}

	public List<CustCard> findByCustIdAndCardType(Long custId, BankCardTypeEnum cardType) {
		CustCard card = new CustCard();
		card.setCustId(custId);
		card.setCardType(cardType.getCode());
		card.setCardStatus(CardStatusEnum.Valid.getCode());
		return this.find(card);
	}

	/**
	 * 根据身份证，卡号，卡类型获取银行卡列表
	 * 
	 * @param custIdno
	 * @param cardNo
	 * @param cardType
	 * @return
	 */
	public List<CustCard> findByCustIdNoAndCardNo(String custIdno, String cardNo, BankCardTypeEnum cardType) {
		CustCard card = new CustCard();
		card.setAccountIdno(custIdno);
		card.setAccountNo(cardNo);
		card.setCardType(cardType.getCode());
		card.setCardStatus(CardStatusEnum.Valid.getCode());
		return this.find(card);
	}

	public CustCard findById(Long cardId) {
		return mapper.findById(cardId);
	}

	@Log
	public CustCardDto queryCardBin(String cardNo) throws CustCardExcetpion {
		BankCardBin bankCardBin = bankCardService.queryCardBin(cardNo);
		BankCardTypeEnum cardType = BankCardTypeEnum.CREDIT;

		// 判断是否借记卡
		if (bankCardBin.getIsCreditCard().indexOf(
				BankCardTypeEnum.DEBIT.getDesc()) > -1) {
			cardType = BankCardTypeEnum.DEBIT;
		}
		if (cardType.getCode() == BankCardTypeEnum.DEBIT.getCode()) {
			// 借记卡
			BankEnum bankEnum = bankCardBin.getBankEnum();
			return new CustCardDto(bankEnum.getCode(), bankEnum.getDesc(),
					cardType);
		} else {
			// 贷记卡
			CreditCardBankEnum creditCardBankEnum = bankCardBin
					.getCreditCardBankEnum();
			return new CustCardDto(creditCardBankEnum.getCode(),
					creditCardBankEnum.getDesc(), cardType);
		}
	}
	
	@Log
	public Boolean checkCustCard(String custName, String custIdNo,
			String accountNo, String mobile, String bankCode)
			throws CustCardExcetpion {
		// 校验验卡四要素是否为空
		if (StringUtils.isBlank(custName) || StringUtils.isBlank(custIdNo)
				|| StringUtils.isBlank(accountNo)
				|| StringUtils.isBlank(mobile)) {
			throw new CustCardExcetpion("用户信息不全");
		}

		return bankCardService.verifyCard(accountNo, custName, custIdNo,
				mobile, EnumUtil.valueOf(BankEnum.class, bankCode, "vbsEnum"),
				source);
	}
	
	@Log
	public CustCardDto bindCustCardAndItemDone(Cust cust, CustCardDto cardDto)
			throws Exception {
		CustCardDto cardDto1 = bindCustCard(cust, cardDto);
		// 认证完成
		creditCatProcessManager.itemDone(
				new AuthItem(AuthItemEnum.DEBIT_CARD.getCode()),
				cust.getLastProccessId());
		authLogService
				.insert(new AuthLog(cust.getLastProccessId(), cust.getCustId(),
						AuthItemEnum.DEBIT_CARD.getCode(), "", new Date()));
		return cardDto1;
	}
	
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public CustCardDto bindCustCard(Cust cust, CustCardDto cardDto)
			throws CustCardExcetpion {

		// 查询该卡号在系统中所有记录
		CustCard custCard = new CustCard();
		List<CustCard> cards = this.findByCustIdNoAndCardNo(
				cust.getCustIdno(), cardDto.getAccountNo(),
				cardDto.getCardType());
		if (cards.size() == 0) {
			custCard.setCustId(cust.getCustId());
			custCard.setAccountIdno(cust.getCustIdno());
			custCard.setAccountName(cust.getCustName());
			custCard.setAccountNo(cardDto.getAccountNo());
			custCard.setBankCode(cardDto.getBankCode());
			custCard.setCardType(cardDto.getCardType().getCode());
			custCard.setCardStatus(CardStatusEnum.Valid.getCode());
			custCard.setReservedMobile(cardDto.getReservedMobile());
			custCard.setIsDefault("0");
			this.insert(custCard);
		} else {
			for (CustCard card : cards) {
				if (card.getCustId().equals(cust.getCustId())) {
					custCard = card;
					break;
				} else {
					throw new CustCardExcetpion("该卡号已被其他账户绑定");
				}
			}
		}
		cardDto.setCardId(custCard.getCardId());
		cardDto.setIsDefault(custCard.getIsDefault().equals("1"));

		return cardDto;
	}

	public List<CustCard> findByCustAndTypeOrderUpdate(Long custId, int type) {
		return mapper.findByCustAndTypeOrderUpdate(custId,type);
	}

}